                .586
                .MMX
                TITLE   CODEC
                .MODEL  FLAT, STDCALL



INTEL_PutDelta_BaseX__NO_SSE			PROTO	C, Dest:DWORD, Top:DWORD, Center:DWORD, Bottom:DWORD, W:DWORD, \
								Quant:DWORD, Rest_Delta:DWORD, Prob:DWORD, Delta:DWORD, NewIndex_X:DWORD

INTEL_PutDelta_BaseX_MinProb__NO_SSE	PROTO	C, Dest:DWORD, Top:DWORD, Center:DWORD, Bottom:DWORD, W:DWORD, \
								Quant:DWORD, Rest_Delta:DWORD, Prob:DWORD, Delta:DWORD, MinProb:DWORD, \
								Delta_Pnt:DWORD, NewIndex_X:DWORD



INTEL_PutDelta_BaseY__NO_SSE			PROTO	C, Dest:DWORD, Sorc:DWORD, Top:DWORD, Bottom:DWORD, W:DWORD, \
								Quant:DWORD, Rest_Delta:DWORD, Prob:DWORD, Delta:DWORD, NewIndex_Y:DWORD

INTEL_PutDelta_BaseY_MinProb__NO_SSE	PROTO	C, Dest:DWORD, Sorc:DWORD, Top:DWORD, Bottom:DWORD, W:DWORD, \
								Quant:DWORD, Rest_Delta:DWORD, Prob:DWORD, Delta:DWORD, MinProb:DWORD, \
								Delta_Pnt:DWORD, NewIndex_Y:DWORD




INTEL_SetProb_BaseX__NO_SSE				PROTO	C, Dest:DWORD, Top:DWORD, Center:DWORD, Bottom:DWORD, W:DWORD, \
								Prob:DWORD, NewIndex_X:DWORD

INTEL_SetProb_BaseX_MinProb__NO_SSE		PROTO	C, Dest:DWORD, Top:DWORD, Center:DWORD, Bottom:DWORD, W:DWORD, \
								Prob:DWORD, ProbLocation:DWORD, MinProb:DWORD, Delta_Pnt:DWORD, Prob_Pnt:DWORD, \
								NewIndex_X:DWORD





INTEL_SetProb_BaseY__NO_SSE				PROTO	C, Top:DWORD, Bottom:DWORD, W:DWORD, Prob:DWORD, NewIndex_Y:DWORD

INTEL_SetProb_BaseY_MinProb__NO_SSE		PROTO	C, Top:DWORD, Bottom:DWORD, W:DWORD, \
								Prob:DWORD, ProbLocation:DWORD, MinProb:DWORD, Delta_Pnt:DWORD, Prob_Pnt:DWORD, \
								NewIndex_Y:DWORD



INTEL_Aver_Line__NO_SSE					PROTO	C, Dest:DWORD, Top:DWORD, Bottom:DWORD, W:DWORD



;====================================================================
;				                                                    
;				SSE - EMULATOR																
;																	
;====================================================================
_PAVGB	MACRO	Dest, Sorc

		MOVQ		MM3,Dest
		MOVQ		MM4,Sorc
		PUNPCKLBW	Dest,MM7
		PUNPCKLBW	MM4,MM7
		PADDW		Dest,MM4
		MOVQ		MM4,Sorc
		PADDW		Dest,[WWWW1]
		PUNPCKHBW	MM3,MM7
		PSRLW		Dest,1
		PUNPCKHBW	MM4,MM7
		PADDW		MM3,MM4
		PADDW		MM3,[WWWW1]
		PSRLW		MM3,1


		PACKUSWB	Dest,MM3

		
ENDM

_PAVGW	MACRO	Dest, Sorc
		PADDW		Dest,Sorc
		PADDW		Dest,[WWWW1]
		PSRLW		Dest,1
ENDM


_PINSRW	MACRO	Dest, Sorc, index
		MOVQ	[Mem64],Dest
		MOV		WORD PTR [Mem64+index*2],BX
		MOV		BX,Sorc
		XCHG	WORD PTR [Mem64+index*2],BX
		MOVQ	Dest,[Mem64]
ENDM

_PEXTRW	MACRO	Dest, Sorc, index
		MOVQ	[Mem64],Sorc
		MOVZX	Dest,WORD PTR [Mem64+index*2]
ENDM
	

;====================================================================
;				                                                    
;				M A C R O																
;																	
;====================================================================

;--------------------------------------------------------------------
;				                                                    
;				Probability																
;																	
;--------------------------------------------------------------------
Probability	MACRO	Base1, Base2, N1, N2, N3, N4, BB, Temp	
LOCAL	Exit
		
		PSLLW	N1,1						;; N1 * 2
		MOVQ	BB,Base1
		PSLLW	N2,1						;; N2 * 2
		PSUBW	Base1,Base2					;; Base1 - Base2
		PSLLW	N3,1						;; N3 * 2
		PADDW	BB,Base2					;; BB = Base1 + Base2	
		PSLLW	N4,1						;; N4 * 2

		PSUBW	N1,BB						;; N1 * 2 - BB
		PXOR	Base2,Base2
		PSUBW	N2,BB						;; N2 * 2 - BB
		PXOR	Temp,Temp
		PSUBW	N3,BB						;; N3 * 2 - BB
		PSUBW	N4,BB						;; N4 * 2 - BB
		PXOR	BB,BB

		;; do abs: 
				
		PCMPGTW	Base2,Base1					;; -1 if <0
		PCMPGTW	Temp,N1
		PCMPGTW	BB,N2

		PXOR	Base1,Base2					;; xor (-1)
		PXOR	N1,Temp
		PXOR	N2,BB

		PSUBW	Base1,Base2					;; xor (-1) +1 = abs  
		PSUBW	N1,Temp						;; abs(N1 * 2 - BB)
		PSUBW	N2,BB						;; abs(N2 * 2 - BB)

		PSLLW	Base1,3						;; abs(Base1 - Base2) * 8				
		PADDW	N1,N2

		PXOR	Base2,Base2
		PXOR	Temp,Temp
		PADDW	Base1,N1

		PCMPGTW	Base2,N3
		PCMPGTW	Temp,N4

		PXOR	N3,Base2
		PXOR	N4,Temp

		PSUBW	N3,Base2					;; abs(N3 * 2 - BB)
		PSUBW	N4,Temp						;; abs(N4 * 2 - BB)
		PADDW	Base1,N3
		PADDW	Base1,N4

		
ENDM

;--------------------------------------------------------------------
;				                                                    
;				PutDeltaX																
;																	
;--------------------------------------------------------------------
PutDeltaX	MACRO	Tail
LOCAL	L, Exit, E

		MOV		EBX,[Top]
		MOV		EDX,[Bottom]
		MOV		ECX,[Center]

		MOVQ	MM0,[ECX+ESI]				;; Center
		PXOR	MM7,MM7
		
		MOVQ	MM5,[EDI+ESI*2]				;; sorc
		MOVQ	MM1,MM0				

		MOVQ	MM2,[EBX+ESI]				;; Top
		PSRLQ	MM1,8						;; Center +1
		
		PSRLW	MM5,8						;; sorc +1,3,5,7
		MOVQ	MM6,MM1						;; Center +1
		
		_PAVGB	MM6,MM0						;; (BB+1)>>1
		PUNPCKLBW	MM0,MM7					;; Center +0 
		
		MOVQ	MM4,[EDX+ESI]				;; Bottom
		PUNPCKLBW	MM6,MM7					

		MOVQ	MM3,MM2						;; Top
		PUNPCKLBW	MM1,MM7					;; Center +1 = Base2	

		PSRLQ	MM3,8						;; Top +1
		PSUBW	MM5,MM6						;; sorc - ((BB+1)>>1) 
		
		MOVQ	[V_BB],MM5					;; sorc - ((BB+1)>>1)
		PUNPCKLBW	MM2,MM7					;; Top +0

		MOVQ	MM5,MM4						;; Bottom
		PSRLQ	MM4,8						;; Bottom +1

		MOVQ	[BB_],MM6					;; (BB+1)>>1
		PUNPCKLBW	MM3,MM7					;; Top +1

		PUNPCKLBW	MM4,MM7					;; Bottom +1
		PUNPCKLBW	MM5,MM7					;; Bottom +0

		Probability	MM0, MM1, MM2, MM3, MM4, MM5,  MM6, MM7		;; return MM0

		MOV		EBX,[Prob]
IFNB	<Tail>
		MOV		ECX,[TailCounter]
ENDIF

		MOVQ	[EBX+ESI*2],MM0
		MOV		EDX,[NewIndex_X]

IRPC	S,<0246>
		MOVZX	EAX,WORD PTR [EBX+ESI*2+S]
		INC		DWORD PTR [EDX+EAX*4]
IFNB	<Tail>
		DEC		ECX
		JE		E
ENDIF
ENDM

E:


		MOV		EBX,[Delta]
		MOV		EDX,[Quant]
		MOV		ECX,[Rest_Delta]


IRPC	S,<0123>
		MOVSX	EAX,WORD PTR [V_BB+S*2]
		_PINSRW	MM0,[ECX+EAX*2],S
		MOV		AX,[EDX+EAX*2]
		MOV		[EBX+ESI+S],AL
IFNB	<Tail>
		DEC		[TailCounter]
		JE		L
ENDIF
ENDM

L:		PADDW	MM0,[BB_]
		MOV		EBX,[Center]
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		;MOVD	MM1,[EBX+ESI]	;This instruction got a syntax error with MASM9.0 (VS2008)
		;						;then replace the following code. (FIXED BY KAZUHY:#VS2008)
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		PUSH	EAX				; 
		MOV		EAX,[EBX+ESI]	;
		MOVD	MM1,EAX			;
		POP		EAX				;
		;;;;;;;;;;;;;;;;;;;;;;;;;
		PACKUSWB	MM0,MM0
		PUNPCKLBW	MM1,MM0
		
IFB		<Tail>
		MOVQ	[EDI+ESI*2],MM1
ELSE

IRPC	SSS, <04>	
		MOVD	EAX,MM1
		PSRLQ	MM1,32
IRPC	S,<0123>
		MOV		[EDI+ESI*2+S+SSS],AL
		SHR		EAX,8
		DEC		[W]
		JE		Exit
ENDM
ENDM

ENDIF
																
Exit:
ENDM		




;--------------------------------------------------------------------
;				                                                    
;				PutDeltaX_MinProb																
;																	
;--------------------------------------------------------------------
PutDeltaX_MinProb	MACRO	Tail
LOCAL	Exit, E

		MOV		EBX,[Top]
		MOV		EDX,[Bottom]
		MOV		ECX,[Center]

		MOVQ	MM0,[ECX+ESI]				;; Center
		PXOR	MM7,MM7
		
		MOVQ	MM5,[EDI+ESI*2]				;; sorc
		MOVQ	MM1,MM0				

		MOVQ	MM2,[EBX+ESI]				;; Top
		PSRLQ	MM1,8						;; Center +1
		
		PSRLW	MM5,8						;; sorc +1,3,5,7
		MOVQ	MM6,MM1						;; Center +1
		
		_PAVGB	MM6,MM0						;; (BB+1)>>1
		PUNPCKLBW	MM0,MM7					;; Center +0 
		
		MOVQ	MM4,[EDX+ESI]				;; Bottom
		PUNPCKLBW	MM6,MM7					

		MOVQ	MM3,MM2						;; Top
		PUNPCKLBW	MM1,MM7					;; Center +1 = Base2	

		PSRLQ	MM3,8						;; Top +1
		PSUBW	MM5,MM6						;; sorc - ((BB+1)>>1) 
		
		MOVQ	[V_BB],MM5					;; sorc - ((BB+1)>>1)
		PUNPCKLBW	MM2,MM7					;; Top +0

		MOVQ	MM5,MM4						;; Bottom
		PSRLQ	MM4,8						;; Bottom +1

		MOVQ	[BB_],MM6					;; (BB+1)>>1
		PUNPCKLBW	MM3,MM7					;; Top +1

		PUNPCKLBW	MM4,MM7					;; Bottom +1
		PUNPCKLBW	MM5,MM7					;; Bottom +0

		Probability	MM0, MM1, MM2, MM3, MM4, MM5,  MM6, MM7		;; return MM0


		MOV		EBX,[Delta]
		MOV		EDX,[Prob]
		MOV		ECX,[Rest_Delta]
		PUSH	ESI
		PUSH	EDI
		MOV		EDI,[Quant]
		MOV		ESI,[NewIndex_X]

		PXOR	MM1,MM1

IRPC	S,<0123>
LOCAL	Skip
		_PEXTRW	EAX,MM0,S
		CMP		EAX,[Min_Probability]
		JL		Skip	
		INC		DWORD PTR [ESI+EAX*4]
		MOV		[EDX],AX
		MOVSX	EAX,WORD PTR [V_BB+S*2]
		_PINSRW	MM1,[ECX+EAX*2],S
		MOV		AX,[EDI+EAX*2]
		MOV		[EBX],AL
		ADD		EDX,2
		INC		EBX

Skip:
IFNB	<Tail>
		DEC		[TailCounter]
		JE		E
ENDIF
ENDM

E:		POP		EDI
		POP		ESI
		MOV		[Delta],EBX
		MOV		[Prob],EDX

		PADDW	MM1,[BB_]
		MOV		EBX,[Center]
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		;MOVD	MM2,[EBX+ESI]	;This instruction got a syntax error with MASM9.0 (VS2008)
		;						;then replace with the following code. (FIXED BY KAZUHY:#VS2008)
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		PUSH	EAX				;
		MOV		EAX,[EBX+ESI]	;
		MOVD	MM2,EAX			;
		POP		EAX				;
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		PACKUSWB	MM1,MM1
		PUNPCKLBW	MM2,MM1
		
IFB		<Tail>
		MOVQ	[EDI+ESI*2],MM2
ELSE

IRPC	SSS, <04>	
		MOVD	EAX,MM2
		PSRLQ	MM2,32
IRPC	S,<0123>
		MOV		[EDI+ESI*2+S+SSS],AL
		SHR		EAX,8
		DEC		[W]
		JE		Exit
ENDM
ENDM

ENDIF
																
Exit:
ENDM		


;--------------------------------------------------------------------
;				                                                    
;				SetProbX																
;																	
;--------------------------------------------------------------------
SetProbX	MACRO	Tail
LOCAL	Exit, E

		MOV		EBX,[Top]
		MOV		EDX,[Bottom]
		MOV		ECX,[Center]

		MOVQ	MM0,[ECX+ESI]				;; Center
		PXOR	MM7,MM7
		
		MOVQ	MM1,MM0				

		MOVQ	MM2,[EBX+ESI]				;; Top
		PSRLQ	MM1,8						;; Center +1
		
		MOVQ	MM6,MM1						;; Center +1
		
		_PAVGB	MM6,MM0						;; (BB+1)>>1
		PUNPCKLBW	MM0,MM7					;; Center +0 
		
		MOVQ	MM4,[EDX+ESI]				;; Bottom

		MOVQ	MM3,MM2						;; Top
		PUNPCKLBW	MM1,MM7					;; Center +1 = Base2	

		PSRLQ	MM3,8						;; Top +1
		
		PUNPCKLBW	MM2,MM7					;; Top +0

		MOVQ	MM5,MM4						;; Bottom
		PSRLQ	MM4,8						;; Bottom +1

		MOVQ	[BB_],MM6					;; (BB+1)>>1
		PUNPCKLBW	MM3,MM7					;; Top +1

		PUNPCKLBW	MM4,MM7					;; Bottom +1
		PUNPCKLBW	MM5,MM7					;; Bottom +0

		Probability	MM0, MM1, MM2, MM3, MM4, MM5,  MM6, MM7		;; return MM0

		MOV		EBX,[Prob]
		MOV		EDX,[NewIndex_X]
IFNB	<Tail>
		MOV		ECX,[TailCounter]
ENDIF

		MOVQ	[EBX+ESI*2],MM0
		

IRPC	S,<0246>
		MOVZX	EAX,WORD PTR [EBX+ESI*2+S]
		INC		DWORD PTR [EDX+EAX*4]
IFNB	<Tail>
		DEC		ECX
		JE		E
ENDIF
ENDM

E:


		MOV		EBX,[Center]
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		;MOVD	MM1,[EBX+ESI]	;This instruction got a syntax error with MASM9.0 (VS2008)
		;						;then replace with the following code. (FIXED BY KAZUHY:#VS2008)
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		PUSH	EAX				;
		MOV		EAX,[EBX+ESI]	;
		MOVD	MM1,EAX			;
		POP		EAX				;
		;;;;;;;;;;;;;;;;;;;;;;;;;
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		;PUNPCKLBW	MM1,[BB_]		;This instruction got a syntax error with MASM9.0 (VS2008)
		;							;then replace with the following code. (FIXED BY KAZUHY:#VS2008)
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		MOVQ		MM0,[BB_]		;
		PUNPCKLBW	MM1,MM0			;
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		
IFB		<Tail>
		MOVQ	[EDI+ESI*2],MM1
ELSE

IRPC	SSS, <04>	
		MOVD	EAX,MM1
		PSRLQ	MM1,32
IRPC	S,<0123>
		MOV		[EDI+ESI*2+S+SSS],AL
		SHR		EAX,8
		DEC		[W]
		JE		Exit
ENDM
ENDM

ENDIF
																
Exit:

ENDM		




;--------------------------------------------------------------------
;				                                                    
;				SetProbX_MinProb																
;																	
;--------------------------------------------------------------------
SetProbX_MinProb	MACRO	Tail
LOCAL	Exit, E

		MOV		EBX,[Top]
		MOV		EDX,[Bottom]
		MOV		ECX,[Center]

		MOVQ	MM0,[ECX+ESI]				;; Center
		PXOR	MM7,MM7
		
		MOVQ	MM1,MM0				

		MOVQ	MM2,[EBX+ESI]				;; Top
		PSRLQ	MM1,8						;; Center +1
		
		MOVQ	MM6,MM1						;; Center +1
		
		_PAVGB	MM6,MM0						;; (BB+1)>>1
		PUNPCKLBW	MM0,MM7					;; Center +0 
		
		MOVQ	MM4,[EDX+ESI]				;; Bottom

		MOVQ	MM3,MM2						;; Top
		PUNPCKLBW	MM1,MM7					;; Center +1 = Base2	

		PSRLQ	MM3,8						;; Top +1
		
		PUNPCKLBW	MM2,MM7					;; Top +0

		MOVQ	MM5,MM4						;; Bottom
		PSRLQ	MM4,8						;; Bottom +1

		MOVQ	[BB_],MM6					;; (BB+1)>>1
		PUNPCKLBW	MM3,MM7					;; Top +1

		PUNPCKLBW	MM4,MM7					;; Bottom +1
		PUNPCKLBW	MM5,MM7					;; Bottom +0

		Probability	MM0, MM1, MM2, MM3, MM4, MM5,  MM6, MM7		;; return MM0


		MOV		EDX,[Prob]
		MOV		ECX,[ProbLocation]
		MOV		EBX,[Prob_Pnt]
		ADD		EBX,ESI
		PUSH	EDI
		MOV		EDI,[NewIndex_X]


IRPC	S,<0123>
LOCAL	Skip
		_PEXTRW	EAX,MM0,S
		CMP		EAX,[Min_Probability]
		JL		Skip	
		MOV		[ECX],EBX
		INC		DWORD PTR [EDI+EAX*4]
		MOV		[EDX],AX
		ADD		DWORD PTR [ECX],S
		ADD		EDX,2
		ADD		ECX,4
		

Skip:
IFNB	<Tail>
		DEC		[TailCounter]
		JE		E
ENDIF
ENDM

E:		POP		EDI
		MOV		[ProbLocation],ECX
		MOV		[Prob],EDX

		MOV		EBX,[Center]
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		;MOVD	MM2,[EBX+ESI]	;This instruction got a syntax error with MASM9.0 (VS2008)
		;						;then replace with the following code. (FIXED BY KAZUHY:#VS2008)
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		PUSH	EAX				; 
		MOV		EAX,[EBX+ESI]	;
		MOVD	MM2,EAX			;
		POP		EAX				;
		;;;;;;;;;;;;;;;;;;;;;;;;;
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		;PUNPCKLBW	MM2,[BB_]	;This instruction got a syntax error with MASM9.0 (VS2008)
		;						;then replace with the following code. (FIXED BY KAZUHY:#VS2008)
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		MOVQ		MM0,[BB_]		;
		PUNPCKLBW	MM2,MM0			;
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		
IFB		<Tail>
		MOVQ	[EDI+ESI*2],MM2
ELSE

IRPC	SSS, <04>	
		MOVD	EAX,MM2
		PSRLQ	MM2,32
IRPC	S,<0123>
		MOV		[EDI+ESI*2+S+SSS],AL
		SHR		EAX,8
		DEC		[W]
		JE		Exit
ENDM
ENDM

ENDIF
																
Exit:
ENDM		


;--------------------------------------------------------------------
;				                                                    
;				PutDeltaY															
;																	
;--------------------------------------------------------------------
PutDeltaY	MACRO	Tail
LOCAL	L, Exit, E
		MOV		EBX,[Top]
		MOV		EDX,[Bottom]
		MOV		ECX,[Sorc]

		MOVQ	MM0,[EBX+ESI-1]
		PXOR	MM7,MM7

		MOVQ	MM1,[EDX+ESI-1]
		MOVQ	MM2,MM0

		MOVQ	MM3,MM0
		PSRLQ	MM0,8

		MOVQ	MM4,MM1
		PSRLQ	MM3,16

		MOVQ	MM5,MM1
		PUNPCKLBW	MM0,MM7					;; Base1	
		
		MOVQ	MM6,MM0
		PUNPCKLBW	MM2,MM7					;; N1	

		PUNPCKLBW	MM3,MM7					;; N2	


		PSRLQ	MM1,8
		PSRLQ	MM5,16

		PUNPCKLBW	MM1,MM7					;; Base2	

		_PAVGW	MM6,MM1						;; (BB+1)>>1
		PUNPCKLBW	MM4,MM7					;; N3	

		MOVQ	[BB_],MM6
		PUNPCKLBW	MM5,MM7					;; N4	


		MOVQ	MM7,[ECX+ESI*2-1]			;; Sorc-1
		PSRLW	MM7,8						;; Sorc

		PSUBW	MM7,MM6
		MOVQ	[V_BB],MM7

		Probability	MM0, MM1, MM2, MM3, MM4, MM5,  MM6, MM7		;; return MM0

		MOV		EBX,[Prob]
		MOV		EDX,[NewIndex_Y]
IFNB	<Tail>

		MOV		ECX,[TailCounter]

IRPC	S,<0123>
		_PEXTRW	EAX,MM0,S
		MOV		WORD PTR [EBX+ESI*2+S*2],AX
		INC		DWORD PTR [EDX+EAX*4]
		DEC		ECX
		JE		E
ENDM

ELSE

		MOVQ	[EBX+ESI*2],MM0

IRPC	S,<0246>
		MOVZX	EAX,WORD PTR [EBX+ESI*2+S]
		INC		DWORD PTR [EDX+EAX*4]
ENDM

ENDIF


E:


		MOV		EBX,[Delta]
		MOV		EDX,[Quant]
		MOV		ECX,[Rest_Delta]


IRPC	S,<0123>
		MOVSX	EAX,WORD PTR [V_BB+S*2]
		_PINSRW	MM0,[ECX+EAX*2],S
		MOV		AX,[EDX+EAX*2]
		MOV		[EBX+ESI+S],AL
IFNB	<Tail>
		DEC		[TailCounter]
		JE		L
ENDIF
ENDM

L:		PADDW	MM0,[BB_]
		PACKUSWB	MM0,MM0
		
IFB		<Tail>
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		;MOVD	[EDI+ESI],MM0	;This instruction got a syntax error with MASM9.0 (VS2008)
		;						;then replace with the following code. (FIXED BY KAZUHY:#VS2008)
		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		PUSH	EAX				; 
		MOVD	EAX,MM0			;
		MOV		[EDI+ESI],EAX	;
		POP		EAX				;
		;;;;;;;;;;;;;;;;;;;;;;;;;
ELSE

		MOVD	EAX,MM0
IRPC	S,<0123>
		MOV		[EDI+ESI+S],AL
		SHR		EAX,8
		DEC		[W]
		JE		Exit
ENDM

ENDIF
																
Exit:
ENDM		


;--------------------------------------------------------------------
;				                                                    
;				PutDeltaY_MinProb															
;																	
;--------------------------------------------------------------------
PutDeltaY_MinProb	MACRO	Tail
LOCAL	L, Exit, E

		MOV		EBX,[Top]
		MOV		EDX,[Bottom]
		MOV		ECX,[Sorc]

		MOVQ	MM0,[EBX+ESI-1]
		PXOR	MM7,MM7

		MOVQ	MM1,[EDX+ESI-1]
		MOVQ	MM2,MM0

		MOVQ	MM3,MM0
		PSRLQ	MM0,8

		MOVQ	MM4,MM1
		PSRLQ	MM3,16

		MOVQ	MM5,MM1
		PUNPCKLBW	MM0,MM7					;; Base1	
		
		MOVQ	MM6,MM0
		PUNPCKLBW	MM2,MM7					;; N1	

		PUNPCKLBW	MM3,MM7					;; N2	


		PSRLQ	MM1,8
		PSRLQ	MM5,16

		PUNPCKLBW	MM1,MM7					;; Base2	

		_PAVGW	MM6,MM1						;; (BB+1)>>1
		PUNPCKLBW	MM4,MM7					;; N3	

		MOVQ	[BB_],MM6
		PUNPCKLBW	MM5,MM7					;; N4	


		MOVQ	MM7,[ECX+ESI*2-1]			;; Sorc-1
		PSRLW	MM7,8						;; Sorc

		PSUBW	MM7,MM6
		MOVQ	[V_BB],MM7

		Probability	MM0, MM1, MM2, MM3, MM4, MM5,  MM6, MM7		;; return MM0

		MOV		EBX,[Delta]
		MOV		EDX,[Prob]
		MOV		ECX,[Rest_Delta]
		PUSH	ESI
		PUSH	EDI
		MOV		EDI,[Quant]
		MOV		ESI,[NewIndex_Y]

		PXOR	MM1,MM1

IRPC	S,<0123>
LOCAL	Skip
		_PEXTRW	EAX,MM0,S
		CMP		EAX,[Min_Probability]
		JL		Skip	
		INC		DWORD PTR [ESI+EAX*4]
		MOV		[EDX],AX
		MOVSX	EAX,WORD PTR [V_BB+S*2]
		_PINSRW	MM1,[ECX+EAX*2],S
		MOV		AX,[EDI+EAX*2]
		MOV		[EBX],AL
		ADD		EDX,2
		INC		EBX

Skip:
IFNB	<Tail>
		DEC		[TailCounter]
		JE		E
ENDIF
ENDM

E:		POP		EDI
		POP		ESI
		MOV		[Delta],EBX
		MOV		[Prob],EDX

		PADDW	MM1,[BB_]
		PACKUSWB	MM1,MM1
		
IFB		<Tail>
		MOVQ	[EDI+ESI],MM1
ELSE

		MOVD	EAX,MM1
IRPC	S,<0123>
		MOV		[EDI+ESI+S],AL
		SHR		EAX,8
		DEC		[W]
		JE		Exit
ENDM

ENDIF
																
Exit:

ENDM


;--------------------------------------------------------------------
;				                                                    
;				SetProbY															
;																	
;--------------------------------------------------------------------
SetProbY	MACRO	Tail
LOCAL	E


		MOV		EBX,[Top]
		MOV		EDX,[Bottom]

		MOVQ	MM0,[EBX+ESI-1]				;; Top
		PXOR	MM7,MM7

		MOVQ	MM1,[EDX+ESI-1]				;; Bottom
		MOVQ	MM2,MM0

		MOVQ	MM3,MM0
		PSRLQ	MM0,8

		MOVQ	MM4,MM1
		PSRLQ	MM3,16

		MOVQ	MM5,MM1
		PUNPCKLBW	MM0,MM7					;; Base1	
		
		PUNPCKLBW	MM2,MM7					;; N1	

		PUNPCKLBW	MM3,MM7					;; N2	


		PSRLQ	MM1,8
		PSRLQ	MM5,16

		PUNPCKLBW	MM1,MM7					;; Base2	

		PUNPCKLBW	MM4,MM7					;; N3	

		PUNPCKLBW	MM5,MM7					;; N4	


		Probability	MM0, MM1, MM2, MM3, MM4, MM5,  MM6, MM7		;; return MM0

		MOV		ECX,[Prob]
		MOV		EDX,[NewIndex_Y]			

IRPC	S,<0123>
		_PEXTRW	EAX,MM0,S
		MOV		[ECX+ESI*2+S*2],AX			;; Prob
		INC		DWORD PTR [EDX+EAX*4]

IFNB	<Tail>
		DEC		[TailCounter]
		JE		E
ENDIF
ENDM

E:		

ENDM		

		

;--------------------------------------------------------------------
;				                                                    
;				SetProbY_MinProb															
;																	
;--------------------------------------------------------------------
SetProbY_MinProb	MACRO	Tail
LOCAL	E

		MOV		EBX,[Top]
		MOV		EDX,[Bottom]

		MOVQ	MM0,[EBX+ESI-1]				;; Top
		PXOR	MM7,MM7

		MOVQ	MM1,[EDX+ESI-1]				;; Bottom
		MOVQ	MM2,MM0

		MOVQ	MM3,MM0
		PSRLQ	MM0,8

		MOVQ	MM4,MM1
		PSRLQ	MM3,16

		MOVQ	MM5,MM1
		PUNPCKLBW	MM0,MM7					;; Base1	
		
		PUNPCKLBW	MM2,MM7					;; N1	

		PUNPCKLBW	MM3,MM7					;; N2	


		PSRLQ	MM1,8
		PSRLQ	MM5,16

		PUNPCKLBW	MM1,MM7					;; Base2	

		PUNPCKLBW	MM4,MM7					;; N3	

		PUNPCKLBW	MM5,MM7					;; N4	


		Probability	MM0, MM1, MM2, MM3, MM4, MM5,  MM6, MM7		;; return MM0


		MOV		EDX,[Prob]
		MOV		ECX,[ProbLocation]
		MOV		EBX,[Prob_Pnt]
		ADD		EBX,ESI
		PUSH	EDI
		MOV		EDI,[NewIndex_Y]	


IRPC	S,<0123>
LOCAL	Skip
		_PEXTRW	EAX,MM0,S
		CMP		EAX,[Min_Probability]
		JL		Skip	
		MOV		[ECX],EBX
		INC		DWORD PTR [EDI+EAX*4]
		MOV		[EDX],AX
		ADD		DWORD PTR [ECX],S
		ADD		EDX,2
		ADD		ECX,4
		

Skip:
IFNB	<Tail>
		DEC		[TailCounter]
		JE		E
ENDIF
ENDM

E:		POP		EDI
		MOV		[ProbLocation],ECX
		MOV		[Prob],EDX



ENDM		



;====================================================================
;
;				D A T A
;
;====================================================================
				.DATA
				ALIGN	8

WWWW1			DQ		1000100010001h



;====================================================================
;
;				C O D E
;
;====================================================================
				.CODE
;___________________________________________________________________________________________________________________________
;
;				INTEL_PutDelta_BaseX
;___________________________________________________________________________________________________________________________
INTEL_PutDelta_BaseX__NO_SSE	PROC	C, Dest:DWORD, Top:DWORD, Center:DWORD, Bottom:DWORD, W:DWORD, \
								Quant:DWORD, Rest_Delta:DWORD, Prob:DWORD, Delta:DWORD, NewIndex_X:DWORD
LOCAL V_BB:QWORD, BB_:QWORD, TailTop[2]:QWORD, TailCenter[2]:QWORD, TailBottom[2]:QWORD, TailCounter:DWORD, \
	  Mem64:QWORD	

		PUSHAD




		MOV		ESI,0
		MOV		EDI,[Dest]

		CMP		[W],9
		JL		@@50
@@5:	PutDeltaX
		ADD		ESI,4
		SUB		[W],8
		CMP		[W],9
		JGE		@@5
		
@@50:	CMP		[W],1
		JE		@@60

		MOV		EAX,[W]
		INC		EAX
		SHR		EAX,1

		MOV		EBX,[Top]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailTop],MM0
		MOV		DL,BYTE PTR [TailTop+EAX-1]
		MOV		BYTE PTR [TailTop+EAX],DL
		LEA		EDX,[TailTop]
		MOV		[Top],EDX
		SUB		[Top],ESI

		MOV		EBX,[Center]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailCenter],MM0
		MOV		DL,BYTE PTR [TailCenter+EAX-1]
		MOV		BYTE PTR [TailCenter+EAX],DL
		LEA		EDX,[TailCenter]
		MOV		[Center],EDX
		SUB		[Center],ESI

		MOV		EBX,[Bottom]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailBottom],MM0
		MOV		DL,BYTE PTR [TailBottom+EAX-1]
		MOV		BYTE PTR [TailBottom+EAX],DL
		LEA		EDX,[TailBottom]
		MOV		[Bottom],EDX
		SUB		[Bottom],ESI

		MOV		EAX,[W]
		SHR		EAX,1
		MOV		[TailCounter],EAX

		PutDeltaX	Tail
		JMP		@@100

@@60:	MOV		EBX,[Center]
		MOV		AL,[EBX+ESI]
		MOV		[EDI+ESI*2],AL


@@100:	POPAD
		EMMS

		RET		

INTEL_PutDelta_BaseX__NO_SSE	ENDP

;___________________________________________________________________________________________________________________________
;
;				INTEL_SetProb_BaseX
;___________________________________________________________________________________________________________________________
INTEL_SetProb_BaseX__NO_SSE	PROC	C, Dest:DWORD, Top:DWORD, Center:DWORD, Bottom:DWORD, W:DWORD, \
							Prob:DWORD, NewIndex_X:DWORD
LOCAL	BB_:QWORD, TailTop[2]:QWORD, TailCenter[2]:QWORD, TailBottom[2]:QWORD, TailCounter:DWORD

		PUSHAD

		MOV		ESI,0
		MOV		EDI,[Dest]

		CMP		[W],9
		JL		@@50
@@5:	SetProbX
		ADD		ESI,4
		SUB		[W],8
		CMP		[W],9
		JGE		@@5
		
@@50:	CMP		[W],1
		JE		@@60

		MOV		EAX,[W]
		INC		EAX
		SHR		EAX,1

		MOV		EBX,[Top]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailTop],MM0
		MOV		DL,BYTE PTR [TailTop+EAX-1]
		MOV		BYTE PTR [TailTop+EAX],DL
		LEA		EDX,[TailTop]
		MOV		[Top],EDX
		SUB		[Top],ESI

		MOV		EBX,[Center]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailCenter],MM0
		MOV		DL,BYTE PTR [TailCenter+EAX-1]
		MOV		BYTE PTR [TailCenter+EAX],DL
		LEA		EDX,[TailCenter]
		MOV		[Center],EDX
		SUB		[Center],ESI

		MOV		EBX,[Bottom]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailBottom],MM0
		MOV		DL,BYTE PTR [TailBottom+EAX-1]
		MOV		BYTE PTR [TailBottom+EAX],DL
		LEA		EDX,[TailBottom]
		MOV		[Bottom],EDX
		SUB		[Bottom],ESI

		MOV		EAX,[W]
		SHR		EAX,1
		MOV		[TailCounter],EAX

		SetProbX	Tail
		JMP		@@100

@@60:	MOV		EBX,[Center]
		MOV		AL,[EBX+ESI]
		MOV		[EDI+ESI*2],AL


@@100:	POPAD
		EMMS

		RET		

INTEL_SetProb_BaseX__NO_SSE	ENDP

;___________________________________________________________________________________________________________________________
;
;				INTEL_PutDelta_BaseX_MinProb
;___________________________________________________________________________________________________________________________
INTEL_PutDelta_BaseX_MinProb__NO_SSE	PROC	C, Dest:DWORD, Top:DWORD, Center:DWORD, Bottom:DWORD, W:DWORD, \
								Quant:DWORD, Rest_Delta:DWORD, Prob:DWORD, Delta:DWORD, Min_Probability:DWORD, \
								Delta_Pnt:DWORD, NewIndex_X:DWORD
LOCAL V_BB:QWORD, BB_:QWORD, TailTop[2]:QWORD, TailCenter[2]:QWORD, TailBottom[2]:QWORD, \
      TailCounter:DWORD, Delta0:DWORD, Mem64:QWORD

		PUSHAD



		PUSH	[Delta]
		POP		[Delta0]

		MOV		ESI,0
		MOV		EDI,[Dest]

		CMP		[W],9
		JL		@@50
@@5:	PutDeltaX_MinProb
		ADD		ESI,4
		SUB		[W],8
		CMP		[W],9
		JGE		@@5
		
@@50:	CMP		[W],1
		JE		@@60

		MOV		EAX,[W]
		INC		EAX
		SHR		EAX,1

		MOV		EBX,[Top]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailTop],MM0
		MOV		DL,BYTE PTR [TailTop+EAX-1]
		MOV		BYTE PTR [TailTop+EAX],DL
		LEA		EDX,[TailTop]
		MOV		[Top],EDX
		SUB		[Top],ESI

		MOV		EBX,[Center]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailCenter],MM0
		MOV		DL,BYTE PTR [TailCenter+EAX-1]
		MOV		BYTE PTR [TailCenter+EAX],DL
		LEA		EDX,[TailCenter]
		MOV		[Center],EDX
		SUB		[Center],ESI

		MOV		EBX,[Bottom]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailBottom],MM0
		MOV		DL,BYTE PTR [TailBottom+EAX-1]
		MOV		BYTE PTR [TailBottom+EAX],DL
		LEA		EDX,[TailBottom]
		MOV		[Bottom],EDX
		SUB		[Bottom],ESI

		MOV		EAX,[W]
		SHR		EAX,1
		MOV		[TailCounter],EAX

		PutDeltaX_MinProb	Tail
		JMP		@@100

@@60:	MOV		EBX,[Center]
		MOV		AL,[EBX+ESI]
		MOV		[EDI+ESI*2],AL


@@100:	MOV		EBX,[Delta_Pnt]
		MOV		EAX,[Delta]
		SUB		EAX,[Delta0]
		ADD		[EBX],EAX

		POPAD
		EMMS

		RET		

INTEL_PutDelta_BaseX_MinProb__NO_SSE	ENDP


;___________________________________________________________________________________________________________________________
;
;				INTEL_SetProb_BaseX_MinProb
;___________________________________________________________________________________________________________________________
INTEL_SetProb_BaseX_MinProb__NO_SSE	PROC	C, Dest:DWORD, Top:DWORD, Center:DWORD, Bottom:DWORD, W:DWORD, \
									Prob:DWORD, ProbLocation:DWORD, Min_Probability:DWORD, Delta_Pnt:DWORD, \
									Prob_Pnt:DWORD, NewIndex_X:DWORD
LOCAL	BB_:QWORD, TailTop[2]:QWORD, TailCenter[2]:QWORD, TailBottom[2]:QWORD, TailCounter:DWORD, \
        Delta0:DWORD, Mem64:QWORD

		PUSHAD



		PUSH	[Prob]
		POP		[Delta0]

		MOV		ESI,0
		MOV		EDI,[Dest]

		CMP		[W],9
		JL		@@50
@@5:	SetProbX_MinProb
		ADD		ESI,4
		SUB		[W],8
		CMP		[W],9
		JGE		@@5
		
@@50:	CMP		[W],1
		JE		@@60

		MOV		EAX,[W]
		INC		EAX
		SHR		EAX,1

		MOV		EBX,[Top]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailTop],MM0
		MOV		DL,BYTE PTR [TailTop+EAX-1]
		MOV		BYTE PTR [TailTop+EAX],DL
		LEA		EDX,[TailTop]
		MOV		[Top],EDX
		SUB		[Top],ESI

		MOV		EBX,[Center]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailCenter],MM0
		MOV		DL,BYTE PTR [TailCenter+EAX-1]
		MOV		BYTE PTR [TailCenter+EAX],DL
		LEA		EDX,[TailCenter]
		MOV		[Center],EDX
		SUB		[Center],ESI

		MOV		EBX,[Bottom]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailBottom],MM0
		MOV		DL,BYTE PTR [TailBottom+EAX-1]
		MOV		BYTE PTR [TailBottom+EAX],DL
		LEA		EDX,[TailBottom]
		MOV		[Bottom],EDX
		SUB		[Bottom],ESI

		MOV		EAX,[W]
		SHR		EAX,1
		MOV		[TailCounter],EAX

		SetProbX_MinProb	Tail
		JMP		@@100

@@60:	MOV		EBX,[Center]
		MOV		AL,[EBX+ESI]
		MOV		[EDI+ESI*2],AL


@@100:	MOV		EBX,[Delta_Pnt]
		MOV		EAX,[Prob]
		SUB		EAX,[Delta0]
		SHR		EAX,1
		ADD		[EBX],EAX

		POPAD
		EMMS

		RET		

INTEL_SetProb_BaseX_MinProb__NO_SSE	ENDP


;___________________________________________________________________________________________________________________________
;
;				INTEL_PutDelta_BaseY
;___________________________________________________________________________________________________________________________
INTEL_PutDelta_BaseY__NO_SSE	PROC	C, Dest:DWORD, Sorc:DWORD, Top:DWORD, Bottom:DWORD, W:DWORD, \
								Quant:DWORD, Rest_Delta:DWORD, Prob:DWORD, Delta:DWORD, NewIndex_Y:DWORD
LOCAL V_BB:QWORD, BB_:QWORD, TailTop[2]:QWORD, TailBottom[2]:QWORD, TailCounter:DWORD, Mem64:QWORD
		
		PUSHAD




		MOV		ESI,0
		MOV		EDI,[Dest]

		CMP		[W],4
		JG		@@5

		MOV		EAX,[W]
		MOV		[TailCounter],EAX

		MOV		EBX,[Top]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailTop],MM0
		MOVQ	[TailTop+1],MM0
		MOV		DL,BYTE PTR [TailTop+1+EAX-1]
		MOV		BYTE PTR [TailTop+1+EAX],DL
		LEA		EDX,[TailTop+1]
		MOV		[Top],EDX
		SUB		[Top],ESI

		MOV		EBX,[Bottom]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailBottom],MM0
		MOVQ	[TailBottom+1],MM0
		MOV		DL,BYTE PTR [TailBottom+1+EAX-1]
		MOV		BYTE PTR [TailBottom+1+EAX],DL
		LEA		EDX,[TailBottom+1]
		MOV		[Bottom],EDX
		SUB		[Bottom],ESI
		PutDeltaY	Tail
		JMP		@@100


@@5:	PUSH	[Top]
		PUSH	[Bottom]

		MOV		EBX,[Top]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailTop],MM0
		MOVQ	[TailTop+1],MM0
		LEA		EDX,[TailTop+1]
		MOV		[Top],EDX
		SUB		[Top],ESI

		MOV		EBX,[Bottom]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailBottom],MM0
		MOVQ	[TailBottom+1],MM0
		LEA		EDX,[TailBottom+1]
		MOV		[Bottom],EDX
		SUB		[Bottom],ESI
		PutDeltaY	
		ADD		ESI,4
		SUB		[W],4
		POP		[Bottom]
		POP		[Top]

		CMP		[W],4
		JLE		@@50

@@10:	PutDeltaY
		ADD		ESI,4
		SUB		[W],4
		CMP		[W],4
		JG		@@10
		
		
@@50:	

		MOV		EAX,[W]
		MOV		[TailCounter],EAX

		MOV		EBX,[Top]
		MOVQ	MM0,[EBX+ESI-1]
		MOVQ	[TailTop],MM0
		MOV		DL,BYTE PTR [TailTop+1+EAX-1]
		MOV		BYTE PTR [TailTop+1+EAX],DL
		LEA		EDX,[TailTop+1]
		MOV		[Top],EDX
		SUB		[Top],ESI

		MOV		EBX,[Bottom]
		MOVQ	MM0,[EBX+ESI-1]
		MOVQ	[TailBottom],MM0
		MOV		DL,BYTE PTR [TailBottom+1+EAX-1]
		MOV		BYTE PTR [TailBottom+1+EAX],DL
		LEA		EDX,[TailBottom+1]
		MOV		[Bottom],EDX
		SUB		[Bottom],ESI
		PutDeltaY	Tail




@@100:	POPAD
		EMMS

		RET		

INTEL_PutDelta_BaseY__NO_SSE	ENDP

;___________________________________________________________________________________________________________________________
;
;				INTEL_PutDelta_BaseY_MinProb
;___________________________________________________________________________________________________________________________
INTEL_PutDelta_BaseY_MinProb__NO_SSE	PROC	C, Dest:DWORD, Sorc:DWORD, Top:DWORD, Bottom:DWORD, W:DWORD, \
								Quant:DWORD, Rest_Delta:DWORD, Prob:DWORD, Delta:DWORD, Min_Probability:DWORD, \
								Delta_Pnt:DWORD, NewIndex_Y:DWORD
LOCAL V_BB:QWORD, BB_:QWORD, TailTop[2]:QWORD, TailBottom[2]:QWORD, TailCounter:DWORD, Delta0:DWORD, \
      Mem64:QWORD

		PUSHAD





		PUSH	[Delta]
		POP		[Delta0]


		MOV		ESI,0
		MOV		EDI,[Dest]

		CMP		[W],4
		JG		@@5

		MOV		EAX,[W]
		MOV		[TailCounter],EAX

		MOV		EBX,[Top]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailTop],MM0
		MOVQ	[TailTop+1],MM0
		MOV		DL,BYTE PTR [TailTop+1+EAX-1]
		MOV		BYTE PTR [TailTop+1+EAX],DL
		LEA		EDX,[TailTop+1]
		MOV		[Top],EDX
		SUB		[Top],ESI

		MOV		EBX,[Bottom]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailBottom],MM0
		MOVQ	[TailBottom+1],MM0
		MOV		DL,BYTE PTR [TailBottom+1+EAX-1]
		MOV		BYTE PTR [TailBottom+1+EAX],DL
		LEA		EDX,[TailBottom+1]
		MOV		[Bottom],EDX
		SUB		[Bottom],ESI
		PutDeltaY_MinProb	Tail
		JMP		@@100


@@5:	PUSH	[Top]
		PUSH	[Bottom]

		MOV		EBX,[Top]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailTop],MM0
		MOVQ	[TailTop+1],MM0
		LEA		EDX,[TailTop+1]
		MOV		[Top],EDX
		SUB		[Top],ESI

		MOV		EBX,[Bottom]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailBottom],MM0
		MOVQ	[TailBottom+1],MM0
		LEA		EDX,[TailBottom+1]
		MOV		[Bottom],EDX
		SUB		[Bottom],ESI
		PutDeltaY_MinProb	
		ADD		ESI,4
		SUB		[W],4
		POP		[Bottom]
		POP		[Top]

		CMP		[W],4
		JLE		@@50

@@10:	PutDeltaY_MinProb
		ADD		ESI,4
		SUB		[W],4
		CMP		[W],4
		JG		@@10
		
		
@@50:	

		MOV		EAX,[W]
		MOV		[TailCounter],EAX

		MOV		EBX,[Top]
		MOVQ	MM0,[EBX+ESI-1]
		MOVQ	[TailTop],MM0
		MOV		DL,BYTE PTR [TailTop+1+EAX-1]
		MOV		BYTE PTR [TailTop+1+EAX],DL
		LEA		EDX,[TailTop+1]
		MOV		[Top],EDX
		SUB		[Top],ESI

		MOV		EBX,[Bottom]
		MOVQ	MM0,[EBX+ESI-1]
		MOVQ	[TailBottom],MM0
		MOV		DL,BYTE PTR [TailBottom+1+EAX-1]
		MOV		BYTE PTR [TailBottom+1+EAX],DL
		LEA		EDX,[TailBottom+1]
		MOV		[Bottom],EDX
		SUB		[Bottom],ESI
		PutDeltaY_MinProb	Tail




@@100:	MOV		EBX,[Delta_Pnt]
		MOV		EAX,[Delta]
		SUB		EAX,[Delta0]
		ADD		[EBX],EAX
		
		POPAD
		EMMS

		RET		

INTEL_PutDelta_BaseY_MinProb__NO_SSE	ENDP



;___________________________________________________________________________________________________________________________
;
;				INTEL_SetProb_BaseY
;___________________________________________________________________________________________________________________________
INTEL_SetProb_BaseY__NO_SSE	PROC	C, Top:DWORD, Bottom:DWORD, W:DWORD, Prob:DWORD, NewIndex_Y:DWORD
LOCAL TailTop[2]:QWORD, TailBottom[2]:QWORD, TailCounter:DWORD, Mem64:QWORD


		PUSHAD


		MOV		ESI,0

		CMP		[W],4
		JG		@@5

		MOV		EAX,[W]
		MOV		[TailCounter],EAX

		MOV		EBX,[Top]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailTop],MM0
		MOVQ	[TailTop+1],MM0
		MOV		DL,BYTE PTR [TailTop+1+EAX-1]
		MOV		BYTE PTR [TailTop+1+EAX],DL
		LEA		EDX,[TailTop+1]
		MOV		[Top],EDX
		SUB		[Top],ESI

		MOV		EBX,[Bottom]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailBottom],MM0
		MOVQ	[TailBottom+1],MM0
		MOV		DL,BYTE PTR [TailBottom+1+EAX-1]
		MOV		BYTE PTR [TailBottom+1+EAX],DL
		LEA		EDX,[TailBottom+1]
		MOV		[Bottom],EDX
		SUB		[Bottom],ESI
		SetProbY	Tail
		JMP		@@100


@@5:	PUSH	[Top]
		PUSH	[Bottom]
		MOV		EBX,[Top]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailTop],MM0
		MOVQ	[TailTop+1],MM0
		LEA		EDX,[TailTop+1]
		MOV		[Top],EDX
		SUB		[Top],ESI

		MOV		EBX,[Bottom]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailBottom],MM0
		MOVQ	[TailBottom+1],MM0
		LEA		EDX,[TailBottom+1]
		MOV		[Bottom],EDX
		SUB		[Bottom],ESI
		
		SetProbY
		
		
		ADD		ESI,4
		SUB		[W],4
		POP		[Bottom]
		POP		[Top]

		CMP		[W],4
		JLE		@@50

@@10:	SetProbY
		ADD		ESI,4
		SUB		[W],4
		CMP		[W],4
		JG		@@10
		
		
@@50:	

		MOV		EAX,[W]
		MOV		[TailCounter],EAX

		MOV		EBX,[Top]
		MOVQ	MM0,[EBX+ESI-1]
		MOVQ	[TailTop],MM0
		MOV		DL,BYTE PTR [TailTop+1+EAX-1]
		MOV		BYTE PTR [TailTop+1+EAX],DL
		LEA		EDX,[TailTop+1]
		MOV		[Top],EDX
		SUB		[Top],ESI

		MOV		EBX,[Bottom]
		MOVQ	MM0,[EBX+ESI-1]
		MOVQ	[TailBottom],MM0
		MOV		DL,BYTE PTR [TailBottom+1+EAX-1]
		MOV		BYTE PTR [TailBottom+1+EAX],DL
		LEA		EDX,[TailBottom+1]
		MOV		[Bottom],EDX
		SUB		[Bottom],ESI
		SetProbY	Tail



@@100:	POPAD
		EMMS

		RET		

INTEL_SetProb_BaseY__NO_SSE	ENDP


;___________________________________________________________________________________________________________________________
;
;				INTEL_SetProb_BaseY_MinProb
;___________________________________________________________________________________________________________________________
INTEL_SetProb_BaseY_MinProb__NO_SSE	PROC	C, Top:DWORD, Bottom:DWORD, W:DWORD, \
									Prob:DWORD, ProbLocation:DWORD, Min_Probability:DWORD, Delta_Pnt:DWORD, \
									Prob_Pnt:DWORD, NewIndex_Y:DWORD
LOCAL TailTop[2]:QWORD, TailBottom[2]:QWORD, TailCounter:DWORD, Delta0:DWORD, Mem64:QWORD

		PUSHAD


		PUSH	[Prob]
		POP		[Delta0]


		MOV		ESI,0

		CMP		[W],4
		JG		@@5

		MOV		EAX,[W]
		MOV		[TailCounter],EAX

		MOV		EBX,[Top]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailTop],MM0
		MOVQ	[TailTop+1],MM0
		MOV		DL,BYTE PTR [TailTop+1+EAX-1]
		MOV		BYTE PTR [TailTop+1+EAX],DL
		LEA		EDX,[TailTop+1]
		MOV		[Top],EDX
		SUB		[Top],ESI

		MOV		EBX,[Bottom]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailBottom],MM0
		MOVQ	[TailBottom+1],MM0
		MOV		DL,BYTE PTR [TailBottom+1+EAX-1]
		MOV		BYTE PTR [TailBottom+1+EAX],DL
		LEA		EDX,[TailBottom+1]
		MOV		[Bottom],EDX
		SUB		[Bottom],ESI
		SetProbY_MinProb	Tail
		JMP		@@100


@@5:	PUSH	[Top]
		PUSH	[Bottom]

		MOV		EBX,[Top]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailTop],MM0
		MOVQ	[TailTop+1],MM0
		LEA		EDX,[TailTop+1]
		MOV		[Top],EDX
		SUB		[Top],ESI

		MOV		EBX,[Bottom]
		MOVQ	MM0,[EBX+ESI]
		MOVQ	[TailBottom],MM0
		MOVQ	[TailBottom+1],MM0
		LEA		EDX,[TailBottom+1]
		MOV		[Bottom],EDX
		SUB		[Bottom],ESI
		SetProbY_MinProb	
		ADD		ESI,4
		SUB		[W],4
		POP		[Bottom]
		POP		[Top]

		CMP		[W],4
		JLE		@@50

@@10:	SetProbY_MinProb
		ADD		ESI,4
		SUB		[W],4
		CMP		[W],4
		JG		@@10
		
		
@@50:	

		MOV		EAX,[W]
		MOV		[TailCounter],EAX

		MOV		EBX,[Top]
		MOVQ	MM0,[EBX+ESI-1]
		MOVQ	[TailTop],MM0
		MOV		DL,BYTE PTR [TailTop+1+EAX-1]
		MOV		BYTE PTR [TailTop+1+EAX],DL
		LEA		EDX,[TailTop+1]
		MOV		[Top],EDX
		SUB		[Top],ESI

		MOV		EBX,[Bottom]
		MOVQ	MM0,[EBX+ESI-1]
		MOVQ	[TailBottom],MM0
		MOV		DL,BYTE PTR [TailBottom+1+EAX-1]
		MOV		BYTE PTR [TailBottom+1+EAX],DL
		LEA		EDX,[TailBottom+1]
		MOV		[Bottom],EDX
		SUB		[Bottom],ESI
		SetProbY_MinProb	Tail




@@100:	MOV		EBX,[Delta_Pnt]
		MOV		EAX,[Prob]
		SUB		EAX,[Delta0]
		SHR		EAX,1
		ADD		[EBX],EAX
		
		POPAD
		EMMS

		RET		

INTEL_SetProb_BaseY_MinProb__NO_SSE	ENDP

;___________________________________________________________________________________________________________________________
;
;				INTEL_Aver_Line
;___________________________________________________________________________________________________________________________
INTEL_Aver_Line__NO_SSE	PROC	C, Dest:DWORD, Top:DWORD, Bottom:DWORD, W:DWORD

		PUSHAD



		MOV		EDI,[Dest]
		MOV		ECX,[W]
		MOVQ	MM6,[EDI+ECX]			; save 8 bytes after end of line
		PXOR	MM7,MM7

		MOV		EDX,[Top]
		MOV		EBX,[Bottom]
		MOV		EAX,0

		ADD		ECX,7
		SHR		ECX,3					; 8-bytes blocks counter

@@5:	MOVQ	MM0,[EDX+EAX]
		_PAVGB	MM0,[EBX+EAX]
		MOVQ	[EDI+EAX],MM0
		ADD		EAX,8
		LOOP	@@5


		MOV		ECX,[W]
		MOVQ	[EDI+ECX],MM6			; restore 8 bytes after end of line
		

		
		POPAD
		EMMS

		RET		

INTEL_Aver_Line__NO_SSE	ENDP


END